package com.tx.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tx.entity.*;
import com.tx.service.*;
import com.tx.utils.FileUtil;
import com.tx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description: 管理员控制器
 * @Date: 2020/05/06
 **/
@Controller("adminController")
@RequestMapping("/admin")
public class AdminView {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UserService userService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private LunBoService lunBoService;
    @Autowired
    private ReplyService replyService;
    /* *
     * @description:
     * @author tcx
     * @date 2020/5/13
     * @param  打开首页
     * @return java.lang.String
     */
    @RequestMapping("/index")
    public String index(){

        return "/admin/index";
    }
    /***************************************博客管理********************************************/
    /* *
     * @description:  打开博客列表
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/blog/list")
    public String blogList(){
        return "/admin/bloglist";
    }

    @RequestMapping("/blog/admin_list")
    @ResponseBody
    public String blogList(@RequestParam(value = "page", required = false) Integer currentPage,
                       @RequestParam(value = "limit", required = false) Integer rows,
                       @RequestParam(value = "keywords", required = false) String keywords,
                       @RequestParam(value = "startTime", required = false) String startTime,
                       @RequestParam(value = "endTime", required = false) String endTime){
        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));
        map.put("startTime", startTime);
        map.put("endTime", StringUtil.formatEndTime(endTime));
        List<Blog> list = blogService.list(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = blogService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");
        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }
    /* *
     * @description:  删除博客
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/blog/admin_delete")
    public String deleteBlog(HttpServletRequest request){
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < idsStr.length; i++) {
            blogService.remove(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }

    /* *
     * @description:  修改博客
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/blog/admin_update")
    public String adminupdate(Blog blog,HttpSession session){
        int resultTotal = blogService.edit(blog);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return gson.toJson(map);
    }
    /* *
     * @description:  打开添加博客
     * @author tcx
     * @date 2020/5/13
     * @param map
     * @return java.lang.String
     */
    @RequestMapping("/blog/to_add")
    public String addBlog(Map<String,Object> map){
        Type type = new Type();
        type.settState(1);
        //查询博客类型信息
        map.put("type",typeService.getAll(type));
        return "/admin/addBlog";
    }
    /* *
     * @description:  添加博客
     * @author tcx
     * @date 2020/5/13
     * @param blog
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/blog/add")
    public String add(Blog blog, HttpSession session){
        //从session里获得登录的用户
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if(user!=null){
            //获取用户id存入数据库
            blog.setuId(user.getuId());
            //添加博客
            int resultTotal=blogService.add(blog);
            if (resultTotal > 0) {
                map.put("code",200);
                map.put("msg","添加成功");
            } else {
                map.put("code",-1);
                map.put("msg","添加失败");
            }
        }else{
            map.put("code",-2);
            map.put("msg","登录失效");
        }

        return gson.toJson(map);
    }
    /* *
     * @description:  打开编辑博客
     * @author tcx
     * @date 2020/5/13
     * @param bId
     * @param map
     * @return java.lang.String
     */
    @RequestMapping(value = "/blog/to_edit",params = "bId")
    public String editBlog(Integer bId,Map<String,Object> map){
        Type type = new Type();
        type.settState(1);
        //获取所有博客类型
        map.put("typelist",typeService.getAll(type));
        //获取博客数据
        map.put("blog",blogService.get(bId));
        return "/admin/editBlog";
    }
    /* *
     * @description: 编辑博客
     * @author tcx
     * @date 2020/5/13
     * @param blog
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/blog/edit")
    public String edit(Blog blog){
        //编辑博客
        int resultTotal = blogService.edit(blog);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",200);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return gson.toJson(map);
    }
    /********************************************评论管理*********************************************/
    /* *
     * @description:  打开评论列表
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/comment/list")
    public String commentList(){
        return "/admin/commList";
    }
    /* *
     * @description:  评论列表
     * @author tcx
     * @date 2020/5/13
     * @param currentPage
     * @param rows
     * @param keywords
     * @param startTime
     * @param endTime
     * @param session
     * @return java.lang.String
     */
    @RequestMapping("/comment/comm_list")
    @ResponseBody
    public String commList(@RequestParam(value = "page", required = false) Integer currentPage,
                              @RequestParam(value = "limit", required = false) Integer rows,
                              @RequestParam(value = "keywords", required = false) String keywords,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime,HttpSession session){

        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));//设置查询条件
        map.put("startTime", startTime);
        map.put("endTime", StringUtil.formatEndTime(endTime));
        List<Comment> list = blogService.getCommentAll(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = blogService.getTotalComm(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");

        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }
    /* *
     * @description:  删除评论
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/comm/admin_delete")
    public String deleteComm(HttpServletRequest request){
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < idsStr.length; i++) {
            blogService.removeComm(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }
    /* *
     * @description:修改评论
     * @author tcx
     * @date 2020/5/13
     * @param comment
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/comm/admin_update")
    public String adminupdate(Comment comment,HttpSession session){
        int resultTotal = blogService.editComm(comment);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }
    /**************************************回复评论管理*****************************************/
    /* *
     * @description:  打开回复评论列表
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/reply/list")
    public String replyList(){
        return "/admin/replyList";
    }
    /* *
     * @description:  回复评论列表
     * @author tcx
     * @date 2020/5/13
     * @param currentPage
     * @param rows
     * @param keywords
     * @param startTime
     * @param endTime
     * @param session
     * @return java.lang.String
     */
    @RequestMapping("/reply/reply_list")
    @ResponseBody
    public String replyList(@RequestParam(value = "page", required = false) Integer currentPage,
                              @RequestParam(value = "limit", required = false) Integer rows,
                              @RequestParam(value = "keywords", required = false) String keywords,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime,HttpSession session){

        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));//设置查询条件
        map.put("startTime", startTime);
        map.put("endTime", StringUtil.formatEndTime(endTime));
        List<Reply> list = replyService.getAll(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = replyService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");

        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }
    /* *
     * @description:  删除评论回复
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/reply/admin_delete")
    public String deleteRep(HttpServletRequest request){
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < idsStr.length; i++) {
            replyService.remove(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }
    /* *
     * @description:修改评论回复
     * @author tcx
     * @date 2020/5/13
     * @param comment
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/reply/admin_update")
    public String updateReply(Reply reply,HttpSession session){
        int resultTotal = replyService.edit(reply);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }
    /***************************************用户管理*****************************************/
    /* *
     * @description:  打开用户列表
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/user/list")
    public String userList(){
        return "/admin/userlist";
    }
    @RequestMapping("/user/admin_list")
    @ResponseBody
    public String list(@RequestParam(value = "page", required = false) Integer currentPage,
                       @RequestParam(value = "limit", required = false) Integer rows,
                       @RequestParam(value = "keywords", required = false) String keywords){
        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));
        //System.out.println("currentPage="+pageBean.getCurrentPage()+"start="+pageBean.getStart()+"size="+ pageBean.getEveryPage()+"q="+StringUtil.formatLike(keywords));
        List<User> list = userService.list(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = userService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");
            //System.out.println("分页");

        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }
    /* *
     * @description:  删除用户
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/user/admin_delete")
    public String delete(HttpServletRequest request){
        //Integer uId = Integer.parseInt(request.getParameter("uId"));
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        //System.out.println("ids="+ids);
        for (int i = 0; i < idsStr.length; i++) {
            userService.remove(Integer.parseInt(idsStr[i]));
            System.out.println(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }
    /***************************************文件管理*****************************************/
    /* *
     * @description:打开文件列表
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/file/list")
    public String fileList(){
        return "/admin/filelist";
    }
    /* *
     * @description:打开文件详情
     * @author tcx
     * @date 2020/5/13
     * @param fId
     * @param map
     * @return java.lang.String
     */
    @RequestMapping(value = "/file/to_info",params = "fId")
    public String fileInfo(Integer fId,Map<String,Object> map){
        //查询出文件详情返回给界面
        map.put("fileinfo",fileInfoService.get(fId));
        return "/admin/fileInfo";
    }
    /* *
     * @description:  打开文件编辑页
     * @author tcx
     * @date 2020/5/13
     * @param fId
     * @param map
     * @return java.lang.String
     */
    @RequestMapping(value = "/file/to_edit",params = "fId")
    public String fileEdit(Integer fId,Map<String,Object> map){
        //查询出文件详情返回给界面
        map.put("file",fileInfoService.get(fId));
        return "/admin/editFile";
    }
    /* *
     * @description:  打开上传文件
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/file/to_add")
    public String addFile(){
        return "/admin/addFile";
    }
    /* *
     * @description:  上传文件
     * @author tcx
     * @date 2020/5/13
     * @param uploadFile
     * @param request
     * @param fileInfo
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping(value = "/file/add")
    public String fileAdd(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletRequest request, FileInfo fileInfo, HttpSession session) throws IOException {
        //从session中获取当前登录用户
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if(user!=null){
            //获得文件
            byte[] buf = uploadFile.getBytes();
            //获取文件名    a.txt
            String originalFileName = uploadFile.getOriginalFilename();
            //获取项目根路径
            String fileUrl = "/";
            fileUrl = request.getServletContext().getRealPath(fileUrl);
            //向url地址存储文件
            String url = FileUtil.writeFileToUrl(uploadFile, fileUrl,originalFileName);
            //向保存文件信息
            fileInfo.setuId(user.getuId());//设置上传文件用户id
            fileInfo.setfSize(FileUtil.readableFileSize(buf.length));//存储文件大小
            fileInfo.setfPath(url);//文件存储路径
            //得到文件类型
            String fType = FileUtil.getType(url);
            fileInfo.setfTypeImage(FileUtil.getImage(fType));
            fileInfo.setfType(fType);//文件类型
            //调用业务层
            int resultTotal=fileInfoService.add(fileInfo);
            if (resultTotal > 0) {
                map.put("code",200);
                map.put("msg","添加成功");
            } else {
                map.put("code",-1);
                map.put("msg","添加失败");
            }
        }else{
            map.put("code",-2);
            map.put("msg","登录失效");
        }
        return gson.toJson(map);
    }

    /* *
     * @description:  文件列表
     * @author tcx
     * @date 2020/5/12
     * @param currentPage
     * @param rows
     * @param keywords
     * @param startTime
     * @param endTime
     * @return java.lang.String
     */
    @RequestMapping("/file/admin_list")
    @ResponseBody
    public String fileList(@RequestParam(value = "page", required = false) Integer currentPage,
                       @RequestParam(value = "limit", required = false) Integer rows,
                       @RequestParam(value = "keywords", required = false) String keywords,
                       @RequestParam(value = "startTime", required = false) String startTime,
                       @RequestParam(value = "endTime", required = false) String endTime){
        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));
        map.put("startTime", startTime);
        map.put("endTime", StringUtil.formatEndTime(endTime));
        List<FileInfo> list = fileInfoService.list(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = fileInfoService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");
        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }

    /* *
     * @description:修改文件名
     * @author tcx
     * @date 2020/5/13
     * @param fileInfo
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/file/admin_update")
    public String adminupdate(FileInfo fileInfo,HttpSession session){
        int resultTotal = fileInfoService.edit(fileInfo);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }
    /* *
     * @description:  编辑文件
     * @author tcx
     * @date 2020/5/13
     * @param uploadFile
     * @param request
     * @param fileInfo
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/file/edit")
    public String update(@RequestParam("uploadFile")MultipartFile uploadFile, HttpServletRequest request,FileInfo fileInfo) throws IOException {
        //如果用户有新上传文件，修改文件路径并删除源文件
        if (uploadFile.getBytes().length!=0){
            //获得文件
            byte[] buf = uploadFile.getBytes();
            //获取文件名    a.txt
            String originalFileName = uploadFile.getOriginalFilename();
            //获取项目根路径
            String fileUrl = "/";
            fileUrl = request.getServletContext().getRealPath(fileUrl);
            //向url地址存储文件
            String url = FileUtil.writeFileToUrl(uploadFile, fileUrl,originalFileName);
            //删除源文件
            FileUtil.deleteFile(fileInfo.getfPath());
            //向保存文件信息
            fileInfo.setfSize(FileUtil.readableFileSize(buf.length));//存储文件大小
            fileInfo.setfPath(url);//文件存储路径
            fileInfo.setfType(FileUtil.getType(url));//文件类型
        }
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        int resultTotal=fileInfoService.edit(fileInfo);
        if (resultTotal > 0) {
            map.put("code",200);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return gson.toJson(map);
    }
    /***************************************轮播图管理*****************************************/
    /* *
     * @description:  打开轮播图管理
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/lunbo/list")
    public String lunBoList(){
        return "/admin/lunBoList";
    }
    /* *
     * @description:  上传轮播图
     * @author tcx
     * @date 2020/5/13
     * @param lunBo
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/lunbo/add")
    public String add(LunBo lunBo, HttpSession session){
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (user!=null){
            //获取用户id存入数据库
            lunBo.setuId(user.getuId());
            //添加博客
            int resultTotal=lunBoService.add(lunBo);
            if (resultTotal > 0) {
                map.put("code",200);
                map.put("msg","添加成功");
            } else {
                map.put("code",-1);
                map.put("msg","添加失败");
            }

        }
        else{
            map.put("code",-2);
            map.put("msg","登录失效");
        }
        return gson.toJson(map);
    }
    /***************************************博客分类管理*****************************************/
    /* *
     * @description:  打开博客分类管理
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/type/list")
    public String blogType(){
        return "/admin/typeList";
    }
    /* *
     * @description:  添加博客分类
     * @author tcx
     * @date 2020/5/13
     * @param type
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/type/add")
    public String addType(Type type){
        System.out.println(type.toString());
        //添加分类
        int resultTotal=typeService.add(type);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",200);
            map.put("msg","添加成功");
        } else {
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return gson.toJson(map);
    }
    /* *
     * @description:  分类禁用
     * @author tcx
     * @date 2020/5/13
     * @param type
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/type/state.do")
    public String adminupdate(Type type,HttpSession session){
        int resultTotal = typeService.state(type);
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            map.put("code",0);
            map.put("msg","修改成功");
            session.removeAttribute("types");
            type.settState(1);
            session.setAttribute("types",typeService.getAll(type));
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }

        return gson.toJson(map);
    }
    /***************************************基本设置*****************************************/
    /* *
     * @description: 打开基本设置
     * @author tcx
     * @date 2020/5/13
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/setting")
    public String set(){
        return "/admin/setting";
    }
    /* *
     * @description:  修改密码
     * @author tcx
     * @date 2020/5/13
     * @param user
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/editPwd")
    public String updatePwd(User user,HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uId", user.getuId());//设置uId
        map.put("uPassword", user.getuPassword());//设置uPwd
        int resultTotal = userService.editPwd(map);
        map.clear();
        Gson gson = new Gson();
        if (resultTotal > 0) {
            session.removeAttribute("user");
            map.put("code",200);
            map.put("msg","修改成功");
        } else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return gson.toJson(map);
    }
    /***************************************退出登录*****************************************/
    /* *
     * @description:  退出登录
     * @author tcx
     * @date 2020/5/13
     * @param session
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        Gson gson = new Gson();
        session.removeAttribute("user");//清空session的内容
        map.put("code",200);
        return gson.toJson(map);
    }
}
